package com.dongdongshop.service.impl;

import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbBrandExample;
import com.dongdongshop.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class BrandServiceImpl implements BrandService {

    private Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }

    @Override
    public boolean addInfo(TbBrand tbBrand) {
        int i = tbBrandMapper.insert(tbBrand);
        return i > 0;
    }

    @Override
    public TbBrand toUpdate(Integer id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        return tbBrand;
    }

    @Override
    public boolean update(TbBrand tbBrand) {
        int i = tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
        return i > 0;
    }

    @Override
    public boolean deleteAll(Integer[] arr) {
        int i = tbBrandMapper.deleteAll(arr);
        return i > 0;
    }

    @Override
    public Page<List<TbBrand>> findPage(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (name != null && name != ""){
            criteria.andNameLike("%" + name + "%");
        }

        List<TbBrand> brandList = tbBrandMapper.selectByExample(example);
        PageInfo info = new PageInfo(brandList);

        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTatal(info.getTotal());
        page.setRows(info.getList());

        return page;
    }


}
